<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 案例列表
//-------------------------

namespace app\admin\controller;

use app\admin\Controller;
use think\Db;
use think\Lang;
use think\Loader;
use think\Exception;

class Banproduct extends Controller
{
    
    protected static $isdelete = false;
    
    use \app\admin\traits\controller\Controller;

    /**
     * 列表
     */
    public function index()
    {
        $model = $this->getModel('pro_recommend');
		//dump($model);die;
        $map = array();
        // id
        if ($this->request->param('id')) $map['n.id'] = ['eq',   $this->request->param('id')  ];
        // 标题
        if ($this->request->param('pro_recom_name')) $map['n.pro_recom_name'] = ['eq',   $this->request->param('pro_recom_name') ];
        // 查询字段
        $field = 'n.id,n.pro_recom_name,n.pro_recom_pic,n.pro_recom_info,n.recom_character1,n.recom_character2,n.recom_character3,n.recom_character4,n.pro_recom_url';
        $listRows = 20;
        // 分页查询
        $list = $model->alias('n')->field($field)
			->where($map)
            ->order('  id desc')->paginate($listRows, false, ['query' => $this->request->get()]);

		
		//print_r($list);//die;
        // 模板赋值显示
        //dump($lsit);die;
        $this->view->assign('list', $list);
        $this->view->assign("page", $list->render());
        $this->view->assign("count", $list->total());
      //  $this->view->assign('numPerPage', $listRows);

        return $this->view->fetch();
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $controller = $this->request->controller();
        $module = $this->request->module();
        if ($this->request->isAjax()) {
            // 更新
            $data = $this->request->post();

            if (!$data['id']) {
                return ajax_return_adv_error("缺少参数ID");
            }
                    
            // 验证
            if (class_exists(Loader::parseClass($module, 'validate', $controller))) {
                $validate = Loader::validate($controller);
                if (!$validate->check($data)) {
                    return ajax_return_adv_error($validate->getError());
                }
            }
			
			
            // 更新数据
            Db::startTrans();
            try {
             //   $data['addtime'] = time();
            // $a= Db::table('jx_pro_recommend")->where(" id = ".$data['id'])->update($data);
           
              	$a = Loader::model("pro_recommend")->save($data,['id'],$data['id']);
				//echo Db::table('jx_product")->getLastSql();
				//print_r($a);die;
                // 提交事务
                Db::commit();
			
                return ajax_return_adv("编辑成功");
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();

                return ajax_return_adv_error($e->getMessage());
            }
        } else {
            // 编辑
            $id = $rId = $this->request->param('id');
			
            if (!$id) {
                throw new Exception("缺少参数ID");
            }
            $vo = $this->getModel("pro_recommend")->find($rId);
			
            if (!$vo) {
                throw new HttpException(404, '该记录不存在');
            }
            $this->view->assign("vo", $vo);
	
            return $this->view->fetch();
        }
    }

    /**
     * 永久删除
     */
//  public function delete()
//  {
//      $model = $this->getModel();
//      $pk = $model->getPk();
//      $ids = $this->request->param($pk);
//      $where[$pk] = ["in", $ids];
//      if (false === $model->where($where)->delete()) {
//          return ajax_return_adv_error($model->getError());
//      }
//
//      return ajax_return_adv("删除成功");
//  }
	    public function delete()
    {
	   	if(request()->isPost()){
				$data = input('post.');
				$res = db('pro_recommend') ->where('id',$data['id'])->delete();
				if($res != false){
					return ajax_return_adv("删除成功");
				}
			}
		
	  	return $this->view->fetch();
	    // return ajax_return_adv("删除成功");
	    
    }

	/**
     * 添加
     */
    public function add()
    {
		if(request()->isPost()){
			$data = input('post.');
			$res = db('pro_recommend') ->insert($data);
			
			return ajax_return_adv("添加成功");
		}
	
        return $this->view->fetch();
    }


















}